package com.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.pojo.Info;
import com.service.IInfoService;
import com.util.Message;

@Controller
public class InfoController {

	@Resource
	private IInfoService infoService;

	// 获取总页数
	@RequestMapping(value = "/getInfoCounts", method = RequestMethod.POST)
	@ResponseBody
	public Message getcounts(@RequestBody Message message) {
		// 获取总数
		int count = infoService.getCounts();
		Message mess = new Message();
		mess.setMessage((int) Math.ceil((float) count / message.getGroups()) + "");
		return mess;
	}

	@RequestMapping(value = "/allInfosByPage", method = RequestMethod.POST)
	@ResponseBody
	public Message getinfos(@RequestBody Message message) {
		List<Info> list = infoService.getInfos(Integer.valueOf(message.getMessage()), message.getGroups());
		// 重新获取总数，保证数据事实刷新
		int count = infoService.getCounts();
		Message mess = new Message();
		mess.setInfo(list);
		mess.setMessage((int) Math.ceil((float) count / message.getGroups()) + "");
		return mess;
	}

	@RequestMapping(value = "/getInfo/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Info getInfoById(@PathVariable int id) {
		return infoService.getInfoById(id);
	}

	@RequestMapping(value = "/deleteInfo/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deleinfo(@PathVariable int id) {
		infoService.deleinfo(id);
		return new Message("success");
	}

	@RequestMapping(value = "/addInfo", method = RequestMethod.POST)
	@ResponseBody
	public Info addinfo(@RequestBody Message message) {
		Info info = infoService.addinfo(message.getInfo().get(0), Integer.valueOf(message.getMessage()));

		return info;
	}
}
